home *** CD-ROM | disk | FTP | other *** search
/ Trading on the Edge / Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin / pc / mac_file / vendor_d / azte_pro / intro.c < prev    next >
Text File  |  1993-06-11  |  6KB  |  279 lines

  1. /*
  2.  * intro.c
  3.  * Converted for hi by Sean Coates, November 1992.
  4.  */
  5.  
  6. #include "define.h"
  7.  
  8. #ifndef NO_STDLIB_H
  9. #include <stdlib.h>
  10. #endif
  11.  
  12. #include <stdio.h>
  13. #include <time.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include <errno.h>
  17.  
  18. #ifndef NO_MALLOC_H
  19. #include <malloc.h>
  20. #endif
  21.  
  22. #define TRUE 1
  23. #define FALSE 0
  24. /* constants */
  25. #define DATA_FILE "tokens.out"    /* file to read token data from */
  26. #define LOG_FILE  "log.out"
  27. #define MAXTOKENS 4          /* max allowable for each buyer/seller */
  28. #define MAXTRIES  2000         /* in data file - must match MAXTOKENS */
  29.                   /* in humgraph.c and maketoks.c */
  30. #define MAXPLAYERS 8
  31.  
  32. /* externals */
  33.  
  34. void open_intro_data();
  35. void intro_chart();
  36.  
  37.  
  38. void show_supply_demand();
  39. int readint();
  40. int gettok();
  41.  
  42.  
  43. /* global variables - besides the general variables all players know about */
  44. FILE *fp, *logfil;               /* global file pointer holds data file */
  45. extern int gametype;
  46. int ntries, ntoks;
  47. int intro_role;
  48.  
  49. int pass=FALSE;
  50. int confirm=TRUE;
  51. int randomized_confirm_keys = TRUE;
  52. int allow_losses = FALSE;
  53.  
  54. typedef struct {
  55.     int    round;
  56.     int    period;
  57.     int    step;
  58.     char    offers[35];
  59.     char    bids[35];
  60.     char    price[10];
  61. } history_rec;
  62.  
  63. history_rec *history_data;
  64.  
  65. int nbuyers, nsellers;
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. /*
  73. *************************************************************************
  74. ** show_supply_demand
  75. ** This function loops though the same token file and
  76. ** displays supply and demand as tables for each set of tokens.
  77. *************************************************************************
  78. */
  79. void show_supply_demand(fp, gametype, ntoks, nbuyers, nsellers, ntries)
  80. FILE *fp;
  81. int gametype, ntoks, nbuyers, nsellers, ntries;
  82. {
  83.     int key,i,j,ok,loop,maxplayers;
  84.     int btoks[MAXTOKENS*MAXPLAYERS], stoks[MAXTOKENS*MAXPLAYERS], eqp, eqt;
  85.     char string[128];
  86.  
  87.     ok = 1;
  88.     if (nsellers>nbuyers)
  89.       maxplayers = nsellers;
  90.     else
  91.       maxplayers = nbuyers;
  92.  
  93.     for(i=0; ((i<ntries) && ok) ; i++)
  94.     {
  95.       for(j=0; ((j < ntoks*nbuyers) && ok) ; j++)
  96.         ok = (ok && (readint(fp,&btoks[j],0,9999) >= 0));
  97.       for(j=0; ((j < ntoks*nsellers) && ok) ; j++)
  98.         ok = (ok && (readint(fp,&stoks[j],0,9999) >= 0));
  99.       ok = (ok && (readint(fp, &eqp, 0, 9999) >= 0));
  100.       ok = (ok && (readint(fp, &eqt, 0, 9999) >= 0));
  101.  
  102. #ifdef LOGFILE
  103.       fprintf(logfil,"Line -> %d , Equil -> %d %d\n",i,eqp,eqt);
  104.       fflush(logfil);
  105. #endif
  106.  
  107.       printf("\n");
  108.       if (ok)
  109.       {
  110.     puts(" ");
  111.         puts("Sample Unit Values Information");
  112.         puts("===================");
  113.         puts("= Buyer  = Seller =");
  114.         puts("===================");
  115.         for(loop=0; loop < ntoks*maxplayers; loop++)
  116.         {
  117.       if(loop<ntoks*nbuyers)
  118.            printf("= %4d   =",btoks[loop]);
  119.       else
  120.         printf("=        =");
  121.       if(loop<ntoks*nsellers)
  122.         printf(" %4d   =\n",stoks[loop]);
  123.       else
  124.         printf("        =\n");
  125.         }
  126.         puts("===================");
  127.         printf("= Equil :  %4d   =\n",eqp);
  128.     printf("= Trades:  %4d   =\n",eqt);
  129.         puts("===================");
  130.       }
  131.       printf("\nWould you like to see another sample (y/n)? ");
  132.       gets(string);
  133.       key = string[0];
  134.       if(key == 'n' || key == 'N')
  135.     return;
  136.     }
  137.  
  138. }
  139.  
  140.  
  141. /* This routines opens fp, it does not close fp! */
  142. void open_intro_data()
  143. {
  144.     history_data = (history_rec *)calloc(50,sizeof(history_rec));
  145.     if (!history_data) {
  146.     fprintf(stderr,"Calloc failed errno -> %d",errno);
  147.     exit(1);
  148.     }
  149.  
  150.     if ((fp = fopen(DATA_FILE,"r")) == NULL) {
  151.         fprintf(stderr,"ERROR: cannot open file %s\n",DATA_FILE);
  152.         exit(1);
  153.     }
  154.  
  155. #ifdef LOGFILE
  156.     if ((logfil = fopen(LOG_FILE,"w")) < 0) {
  157.     fprintf(stderr,"ERROR: cannot open file %s\n",LOG_FILE);
  158.         exit(1);
  159.     }
  160. #endif
  161.  
  162.     if (readint(fp,&gametype,0,9999) < 0) {
  163.     fprintf(stderr,"ERROR: unable to read gametype from file %s\n",DATA_FILE);
  164.         exit(1);
  165.     }
  166.  
  167.     if (readint(fp,&ntoks,0,MAXTOKENS) < 0) {
  168.     fprintf(stderr,"ERROR: unable to read ntoks from file %s\n",DATA_FILE);
  169.         exit(1);
  170.     }
  171.     if (ntoks > MAXTOKENS) {
  172.     fprintf(stderr,"ERROR: ntokens > MAXTOKENS in data file %s\n",DATA_FILE);
  173.     exit(1);
  174.     }
  175.  
  176.     if (readint(fp,&nbuyers,0,MAXPLAYERS) < 0) {
  177.     fprintf(stderr,"ERROR: unable to read nbuyers from file %s\n",DATA_FILE);
  178.         exit(1);
  179.     }
  180.     if (nbuyers > MAXPLAYERS) {
  181.     fprintf(stderr,"ERROR: nbuyers > MAXPLAYERS in data file %s\n",DATA_FILE);
  182.     exit(1);
  183.     }
  184.  
  185.     if (readint(fp,&nsellers,0,MAXPLAYERS) < 0) {
  186.     fprintf(stderr,"ERROR: unable to read nsellers from file %s\n",DATA_FILE);
  187.         exit(1);
  188.     }
  189.     if (nsellers > MAXPLAYERS) {
  190.     fprintf(stderr,"ERROR: nsellers > MAXPLAYERS in data file %s\n",DATA_FILE);
  191.     exit(1);
  192.     }
  193.     if (readint(fp,&ntries,0,9999) < 0) {
  194.     fprintf(stderr,"ERROR: unable to read ntries from file %s\n",DATA_FILE);
  195.         exit(1);
  196.     }
  197.     if (ntries > MAXTRIES) {
  198.     fprintf(stderr,"ERROR: ntries > MAXTRIES in data file %s\n",DATA_FILE);
  199.     exit(1);
  200.     }
  201.  
  202. } /* open_intro_data */
  203.  
  204. void intro_chart()
  205. {
  206.     int oldcolor;
  207.     char string[128];
  208.  
  209.         open_intro_data();
  210.     puts("The table below shows sample buyers' and sellers' units.  The");
  211.     puts("approximate equilibrium value will also be displayed.  These values");
  212.     puts("were chosen in the same random way as the values of the units in the");
  213.     puts("experiment you will be playing.");  
  214.     printf("\nPress Enter/Return to continue...\n");
  215.     gets(string);
  216.     fflush(stdin);
  217.     show_supply_demand(fp,gametype,ntoks,nbuyers,nsellers,ntries);
  218.     fclose(fp);
  219.     free(history_data);
  220.     fflush(stdin);
  221.  
  222. } /* intro_chart */
  223.  
  224. int readint(fp,variable,minval,maxval)
  225. FILE *fp;
  226. int *variable;
  227. int minval,maxval;
  228. {
  229.     char buf[10],*ptr,c;
  230.     int answer,status;
  231.  
  232.     status = gettok(fp,buf,10);
  233.     if (status==0) {
  234.         ptr = buf;
  235.         while ((c= *ptr++) != '\0' && isdigit(c));
  236.         answer = atoi(buf);
  237.         if(c!='\0')
  238.             return -1;
  239.         else {
  240.             if (answer<minval)
  241.                 return -1;
  242.             else if (answer>maxval)
  243.                 return -1;
  244.             else {
  245.                 *variable = answer;
  246.                 return answer;
  247.             }
  248.         }
  249.     }
  250.     else return -1;
  251.  
  252. } /* readint */
  253.  
  254. int gettok(fp,string,len)
  255. FILE *fp;
  256. char *string;
  257. int len;
  258. {
  259.     int c;
  260.  
  261.     if (len<=0) return 1;
  262.     while ((c=getc(fp))!=EOF) {
  263.         if (c == '#')
  264.             while ((c=getc(fp))!=EOF && c!='\n') ;
  265.         if (!isspace(c))
  266.             break;
  267.     }
  268.     if (c==EOF)
  269.         return -1;
  270.     if (--len>0) *string++ = (char)c;
  271.     while ((c=getc(fp))!=EOF && !isspace(c))
  272.         if (--len>0) *string++ = (char)c;
  273.     if (c!=EOF)
  274.         ungetc(c,fp);
  275.     *string = '\0';
  276.     return (len<=0);
  277.  
  278. } /* gettok */
  279.